<?php

class Bootstrap {

    function __construct() {
       
        $url = $_GET['url'];
        $url = rtrim($url, '/');
        
        if (empty($url))
            $url = 'login';

        $url = explode('/', $url);
        
        $file = 'controller/' . $url[0] . '.php';

        //Check URL-File part
        if (!file_exists($file)) {
            unset($url);
            $url[0] = 'error';
            $url[1] = 'controller_not_found';
            $url[2] = $file;
            $file = 'controller/error.php';
        }

        //TODO Function with error doesnt give the right page       
        require $file;

        //Create controller
        $controller = new $url[0];
        $controller->LoadModel($url[0]);
        
       //Check if Function exist
        if (count($url) > 1 && !method_exists($controller, $url[1])) {
            unset($url);
            $url[0] = 'error';            
            $url[2] = $file.'/'.$url[1];
            $url[1] = 'function_not_found';
            $file = 'controller/error.php';
            
            require $file;
            $controller = new $url[0];
            $controller->LoadModel($url[0]);
        }

        switch (count($url)) {
            //No function
            case 1:
                $controller->index();
                break;

            //Function with no argument
            case 2:
                
                $controller->$url[1]();
                break;

            //Function with argument
            case 3:               
                $controller->$url[1]($url[2]);
                break;
        }
    }

}